package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.pojo.result.Result;
import com.itheima.health.pojo.vo.MemberReportVO;
import com.itheima.health.pojo.vo.SetmealReport;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {


    @Autowired
    private ReportService reportService;

    /**
     * 查询报表数据
     * @return
     */
    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData() {
        log.info("查询报表数据");
        Map<String, Object> result=reportService.getBusinessReportData();
        return new Result(true, "查询成功", result);
    }

    /**
     * 查询套餐数据
     * @return
     */
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport() {
        SetmealReport setmealReport = reportService.getSetmealReport();
        return new Result(true, MessageConst.QUERY_SETMEAL_SUCCESS, setmealReport);
    }

    /**
     * 导出报表数据
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("导出报表数据");
        // 查询报表数据
        Map<String, Object> result=reportService.getBusinessReportData();
        // 导出报表数据,准备写入excel报表当中
        String reportData=(String) result.get("reportDate");
        String year = reportData.substring(0, 4);
        String month = reportData.substring(4, 6);
        String day = reportData.substring(6, 8);
        String reportDate = year + "-" + month + "-" + day;
        Integer todayNewMember=(Integer) result.get("todayNewMember");
        Integer totalMember=(Integer) result.get("totalMember");
        Integer thisWeekNewMember=(Integer) result.get("thisWeekNewMember");
        Integer thisMonthNewMember=(Integer) result.get("thisMonthNewMember");
        Integer todayOrderNumber=(Integer) result.get("todayOrderNumber");
        Integer thisWeekOrderNumber=(Integer) result.get("thisWeekOrderNumber");
        Integer thisMonthOrderNumber=(Integer) result.get("thisMonthOrderNumber");
        Integer todayVisitsNumber=(Integer) result.get("todayVisitsNumber");
        Integer thisWeekVisitsNumber=(Integer) result.get("thisWeekVisitsNumber");
        Integer thisMonthVisitsNumber=(Integer) result.get("thisMonthVisitsNumber");
        List<Map> hotSetmeal=(List<Map>) result.get("hotSetmeal");

        //读取模板文件的绝对路径
//        String temlateRealPath=request.getSession().getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
//       String temlateRealPath="D:\\Java\\code\\itcast-health-cooperation\\src\\main\\resources\\template\\report_template.xlsx";
        InputStream temlateRealPath=this.getClass().getClassLoader().getResourceAsStream("template/report_template.xlsx");
        //读取模板创建excel对象
        XSSFWorkbook workbook=new XSSFWorkbook(temlateRealPath);
        XSSFSheet sheet=workbook.getSheetAt(0);//生成工作表
        //日期
        XSSFRow rows=sheet.getRow(2);
        rows.getCell(5).setCellValue(reportDate);
        rows=sheet.getRow(4);
        rows.getCell(5).setCellValue(todayNewMember);
        rows.getCell(7).setCellValue(totalMember);
        rows=sheet.getRow(5);
        rows.getCell(5).setCellValue(thisWeekNewMember);
        rows.getCell(7).setCellValue(thisMonthNewMember);
        rows=sheet.getRow(7);
        rows.getCell(5).setCellValue(todayOrderNumber);
        rows.getCell(7).setCellValue(todayVisitsNumber);
        rows=sheet.getRow(8);
        rows.getCell(5).setCellValue(thisWeekOrderNumber);
        rows.getCell(7).setCellValue(thisWeekVisitsNumber);
        rows=sheet.getRow(9);
        rows.getCell(5).setCellValue(thisMonthOrderNumber);
        rows.getCell(7).setCellValue(thisMonthVisitsNumber);
        for (int i=0; i < hotSetmeal.size(); i++) {
            rows=sheet.getRow( 12+ i);
            rows.getCell(4).setCellValue(hotSetmeal.get(i).get("name").toString());
            rows.getCell(5).setCellValue(hotSetmeal.get(i).get("setmeal_count").toString());
            rows.getCell(6).setCellValue(hotSetmeal.get(i).get("proportion").toString());
        }
        //通过输出流进行文件下载
        ServletOutputStream outputStream=response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
        workbook.write(outputStream);
        //关闭资源
        outputStream.flush();
        outputStream.close();
        workbook.close();
        return null;
    }

    /**
     * 统计分析-会员数量折线图接口
     * @return
     */
    @GetMapping("/getMemberReport")
    public Result getMemberReport(){
        log.info("近一年会员数量统计");
       MemberReportVO memberReportVO = reportService.getMemberReport();
       return new Result(true, MessageConst.GET_MEMBER_NUMBER_REPORT_SUCCESS, memberReportVO);
    }



}
